<?php

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/pages/vpage/vpt/vpage_manage_wp_abs.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_normal_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_images_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');


class VPage_Man_Normal_WP extends VPage_Manage_WP_Abs {
	protected function _manage() {
		$vpage = $this->get_vpage();

		$title = $this->load_system_attribute('title');
		$this->export('title', $title);

		$meta_description = $this->load_system_attribute('meta_description');
		$this->export('meta_description', $meta_description);

		$content_ci = $this->load_content_item('content');
		if ($content_ci) {
			$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
			$dbc = $this->get_dbc();
			if ($ci_normal_ci->load_by_id($dbc, $content_ci->get_ci_id())) {
				$this->export('content', $this->prepare_normal_ci($ci_normal_ci));
				$this->export_images($dbc, $content_ci->get_id());
				$this->export_attachments($dbc, $content_ci->get_id());
				$this->export('ci_id', $content_ci->get_ci_id());
			}
		}

		if ($title && $content_ci) {
			$this->export('can_go_online', 1);
		}

		$view = $this->get_view('default');

		return $view;
	}


	private function prepare_normal_ci(TCMS_CI_Normal_CI_DBC $ci_normal_ci) {
		$ret['id'] = $ci_normal_ci->get_ci()->get_id();
		$ret['content'] = $ci_normal_ci->get_content()->get_value();
		$ret['created'] = $ci_normal_ci->get_ci()->get_created();

		return $ret;
	}


	protected function export_images(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$ci_images = TCMS_CI_Image_DBC::load_ci_images($dbc, $ci_id);
		$images = array();
		foreach ($ci_images as  $cii) {
			$tmp = new TCMS_Image_DBC();
			if ($tmp->load_by_id($dbc, $cii->get_image())) {
				$images[] = clone $tmp;
			}
		}
		$this->export('images', $images);


		$settings_images = new TCMS_Settings_Images_DBC();
		$settings_images->load_by_id($dbc, 1);
		$this->export('t1_width', $settings_images->get_thumbnail1_size_x());
		$this->export('t1_height', $settings_images->get_thumbnail1_size_y());
		$this->export('t2_width', $settings_images->get_thumbnail2_size_x());
		$this->export('t2_height', $settings_images->get_thumbnail2_size_y());
	}


	protected function export_attachments(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$ci_attachments = TCMS_CI_Attachment_DBC::load_ci_attachments($dbc, $ci_id);
		$attachments = array();
		foreach ($ci_attachments as  $ci) {
			$tmp = new TCMS_Attachment_DBC();
			if ($tmp->load_by_id($dbc, $ci->get_attachment())) {
				$attachments[] = clone $tmp;
			}
		}
		$this->export('attachments', $attachments);
	}
}